package casa.depesos.app.com.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.BankCardActivity;
import casa.depesos.app.com.databinding.BankCardAddBinding;
import casa.depesos.app.com.databinding.BankCardItemBinding;
import casa.depesos.app.com.http.data.BankCard;

public class BankCardAdapter extends RecyclerView.Adapter {

    List<BankCard> list;
    BankCardActivity activity;

    public BankCardAdapter(BankCardActivity activity, List<BankCard> list) {
        this.activity = activity;
        this.list = list;
    }

    public void setData(List list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void delete(int p) {
        list.remove(p);
        notifyItemRemoved(p);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            BankCardItemBinding binding = BankCardItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ItemHolder(binding);
        } else {
            BankCardAddBinding binding = BankCardAddBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new LastHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemHolder) {
            ((ItemHolder) holder).bind(position);
        } else if (holder instanceof LastHolder) {
            ((LastHolder) holder).bind();
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 1 : list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return 1;
        }
        return 0;
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        BankCardItemBinding binding;

        ItemHolder(BankCardItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            BankCard item = list.get(p);
            binding.name.setText(item.name);
            binding.num.setText(item.cardNo);
            if ("1".equalsIgnoreCase(item.type)) {
                binding.desc.setText("CLABE");
            } else {
                binding.desc.setText("Tarjeta de Debito");
            }
            if ("y".equalsIgnoreCase(item.isDefault)) {
                binding.moren.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.checked_white, 0, 0, 0);
            } else {
                binding.moren.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.uncheck_white, 0, 0, 0);
            }
            binding.delete.setOnClickListener(v -> {
                activity.delete(item, getBindingAdapterPosition());
            });
            binding.moren.setOnClickListener(v -> {
                activity.setDefault(item);
            });
            itemView.setOnClickListener(v -> {
                activity.selectCard(item);
            });
        }
    }

    class LastHolder extends RecyclerView.ViewHolder {
        BankCardAddBinding binding;

        LastHolder(BankCardAddBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind() {
            itemView.setOnClickListener(v -> {
                activity.add();
            });
        }
    }
}
